/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.hobbiestag;

import com.apt.bean.Hobbies;
import com.apt.bean.Liking;
import com.apt.hobbiestag.process.Process_PageHobbies;
import java.util.HashMap;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Bach
 */
public class HobbiesTagHandler extends SimpleTagSupport {

    private String page;
    private String liking_Model_Name;
    private String liking_Model_Id;

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        int cursor1;
        HashMap<Integer, Liking> hm = GetHobbies.getListHobbies();
        try {
            if (hm != null && hm.size() != 0) {
                out.println(Process_PageHobbies.divPage(getPage(), hm.size(), 16, liking_Model_Name, liking_Model_Id));
                int uid_f = 1;
                int uid_l = 30;

                try {
                    cursor1 = Integer.parseInt(getPage());

                } catch (NumberFormatException ex) {
                    cursor1 = 1;
                }
                if (cursor1 > 1) {
                    uid_f = (cursor1 - 1) * 30;
                    uid_l = cursor1 * 30;
                }
                int current_page = uid_l / 30;
                for (int i = uid_f; i < uid_l; i++) {
                    Liking u = hm.get(i);
                    if (u != null) {
                        int status = Integer.parseInt(u.getLiking_Status());
                        String liking_Name = u.getLiking_Name();
                        String liking_Id = u.getLiking_Id();
                        StringBuilder sb = new StringBuilder(" <tr>");
                        // sb.append(" <td><input type='checkbox' /></td>");
//                        sb.append("<td>" + "</td>");<td><input  type=\"text\" value=\"""\" name=\"\"/></td>
//<input type="text"
                        if (status == 1) {
                            sb.append("<form action=\"EditHobbiesServle\" method=\"get\">\n"
                                    + " <td> \n"
                                    + "<input type=\"text\"  name=\"liking_Name\" value=\"" + u.getLiking_Name() + "\"/>\n"
                                    + "<input type=\"submit\" value=\"Update Name\"/>\n"
                                    + "<input type=\"hidden\" name=\"liking_Id\" value=\""+liking_Id+"\"   />"
                                    + "<input type=\"hidden\" name=\"pageno\" value=\""+current_page+"\"   />"
                                    + " </td>\n"
                                    + "</form>");
                            sb.append("  <td><a href=\"#\" title=\"title\">Enable</a></td>");
                        } else {
                            sb.append("<td>" + u.getLiking_Name() + "</td>");
                            sb.append("  <td><a href=\"#\" title=\"title\">Disable</a></td>");
                        }
                        sb.append("<td>");
                        sb.append("  <!-- Icons -->");
                        if (status == 1) {
                            u.getLiking_Id();
                            sb.append("<a href=\"DisableHobbiesServlet?pageno=" + current_page + "&hobbiesname=" + liking_Name + "&hobbieId=" + liking_Id + "&likingId=" + u.getLiking_Id() + "\" title=\"Disable\"><img src=\"resources/images/icons/cross.png\" alt=\"Disable\" /></a>");
                            sb.append("---");
                            sb.append("  <a href=\"preferences_edit.jsp?"
                                    + "liking_Id=" + u.getLiking_Id() + "&liking_Model_Name=" + liking_Name + ""
                                    + "&liking_Model_Status=" + u.getLiking_Status() + "&liking_Model_Id=" + liking_Id + ""
                                    + "&pageno=" + current_page + "\" \" title=\"Edit\">"
                                    + "<img src=\"resources/images/icons/edit.jpg\" alt=\"Enable\" /></a>");
                        } else {
                            sb.append("<a href=\"EnableHobbiesServlet?pageno=" + current_page + "&hobbiesname=" + liking_Name + "&hobbieId=" + liking_Id + "&likingId=" + u.getLiking_Id() + "\" title=\"Enable\"><img src=\"resources/images/icons/pencil.png\" alt=\"Enable\" /></a>");
                        }
//                        sb.append(" <a href=\"Navigation?menu=17&pageno=" + current_page +"&payname="+u.getPayment_Type_Name()+ "\" title=\"Enable\" ><img src=\"resources/images/icons/view.png\" alt=\"Enable\" /></a>");
                        sb.append("   </td>");
                        sb.append(" </tr>");
                        out.print(sb.toString());

                    }
                }
            }
            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }

        } catch (java.io.IOException ex) {
            throw new JspException("Error in ProfessionnalTagHandler tag", ex);
        }
    }

    public void setPage(String page) {
        this.page = page;
    }

    /**
     * @return the page
     */
    public String getPage() {
        return page;
    }

    /**
     * @return the liking_Model_Name
     */
    public String getLiking_Model_Name() {
        return liking_Model_Name;
    }

    /**
     * @param liking_Model_Name the liking_Model_Name to set
     */
    public void setLiking_Model_Name(String liking_Model_Name) {
        this.liking_Model_Name = liking_Model_Name;
    }

    /**
     * @return the liking_Model_Id
     */
    public String getLiking_Model_Id() {
        return liking_Model_Id;
    }

    /**
     * @param liking_Model_Id the liking_Model_Id to set
     */
    public void setLiking_Model_Id(String liking_Model_Id) {
        this.liking_Model_Id = liking_Model_Id;
    }
}
